package com.example.demo.service;

import com.example.demo.aop.DistributedLock;
import com.example.demo.aop.LockManager;
import com.example.demo.dto.StudentDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = {"studentCache"})
public class StudentCacheService {

    @Cacheable(key = "'student:' + #p0 + #p1", unless = "#result == null")
    public StudentDTO findByHotelCode(String studentNo, String classNo) {
        return StudentDTO.builder().studentNo("20080212").age(18).classNo("3001").name("张三").build();
    }

    @DistributedLock(value = "testLock", key = "#dto.studentNo + #dto.name")
    public StudentDTO getStudent(StudentDTO dto, String studentNo) {
        System.out.println("进入方法----");
        System.out.println("获取锁：" + LockManager.getLock());
        if (StringUtils.isNotBlank(LockManager.getLock())) {
            throw new RuntimeException();
        }
        return StudentDTO.builder().studentNo("20080212").age(18).classNo("3001").name("张三").build();
    }
}
